//
//  AppDelegate.m
//  BP_NewClient
//
//  Created by normal on 15-1-19.
//  Copyright (c) 2015年 normal. All rights reserved.
//

#import "AppDelegate.h"
#import "BP_NewClient.pch"
#import "BP_NewClient-Swift.h"
//#import <ShareSDK/ShareSDK.h>
#import "ChuaiGuo.h"
//#import "WXApi.h"
#import "APIKey.h"
#import "MAMapKit/MAMapKit.h"

#import "BPCityListViewController.h"
//#define appKey @"25a64c839b5f"
//#define appSecret @"9a639150fcb464d9a1c1ab926648ca3f"
#import "HomeViewController.h"
#import "OrderViewController.h"
#import "MyViewController.h"

#import "BPCategoryViewController.h"
#import "SubmitOrderViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
@synthesize window;


- (void)configureAPIKey
{
    if ([APIKey length] == 0)
    {
        NSString *reason = [NSString stringWithFormat:@"apiKey为空，请检查key是否正确设置。"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:reason delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    
    [MAMapServices sharedServices].apiKey = (NSString *)APIKey;
}

+(AppDelegate *)shareApplication{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 

    // Override point for customization after application launch.
//    [SMS_SDK registerApp:@"25a64c839b5f" withSecret:@"9a639150fcb464d9a1c1ab926648ca3f"];
//    [ShareSDK registerApp:@"4a88b2fb067c"];
    [[UINavigationBar appearance] setBarTintColor:[BPCommon colorWithHexValue:0xFFFFFF alpha:1.0f]];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [self configureAPIKey];

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];

//    [ShareSDK connectWeChatWithAppId:@"wx4868b35061f87885"
//                           appSecret:@"64020361b8ec4c99936c0e3999a9f249"
//                           wechatCls:[WXApi class]];
    
   __block NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
    NSDictionary *cityModel = [standard objectForKey:@"reginInfo"];
   
//    if (cityModel) {
        [self setTabbarController];
        self.window.rootViewController = leveyTabBarController;
//    }else{
//        BPCityListViewController *cityList = [[BPCityListViewController alloc] init];
//        cityList.cityBlock = ^(CityModel *model){
//
//            [standard setObject:[model toDictionary] forKey:@"reginInfo"];
//            [standard synchronize];
//
//            [self setTabbarController];
//            self.window.rootViewController = leveyTabBarController;
//        };
//        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:cityList];
//        self.window.rootViewController = nav;
//    }


    /*
    ProductDetailViewController *productDetail = [[ProductDetailViewController alloc] initWithNibName:@"ProductDetailViewController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:productDetail];
    self.window.rootViewController = nav;
    */
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)setTabbarController{
    HomeViewController *home = [[HomeViewController alloc] init];
    OrderViewController *order = [[OrderViewController alloc] initWithNibName:@"OrderViewController" bundle:nil];
    MyViewController *my = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
    
    UINavigationController *nc_home = [[UINavigationController alloc] initWithRootViewController:home];
    UINavigationController *nc_order = [[UINavigationController alloc] initWithRootViewController:order];
    UINavigationController *nc_my = [[UINavigationController alloc] initWithRootViewController:my];
    
    nc_home.interactivePopGestureRecognizer.enabled = YES;
    nc_order.interactivePopGestureRecognizer.enabled = YES;
    nc_my.interactivePopGestureRecognizer.enabled = YES;
    
    NSArray *ctrlArr = [NSArray arrayWithObjects:nc_home,nc_order,nc_my,nil];
    
    NSMutableDictionary *imgDic = [NSMutableDictionary dictionaryWithCapacity:3];
    [imgDic setObject:[UIImage imageNamed:@"home_normal"] forKey:@"Default"];
    [imgDic setObject:[UIImage imageNamed:@"home_select"] forKey:@"Highlighted"];
    [imgDic setObject:[UIImage imageNamed:@"home_select"] forKey:@"Seleted"];
    
    NSMutableDictionary *imgDic2 = [NSMutableDictionary dictionaryWithCapacity:3];
    [imgDic2 setObject:[UIImage imageNamed:@"order_normal"] forKey:@"Default"];
    [imgDic2 setObject:[UIImage imageNamed:@"order_select"] forKey:@"Highlighted"];
    [imgDic2 setObject:[UIImage imageNamed:@"order_select"] forKey:@"Seleted"];
    
    NSMutableDictionary *imgDic3 = [NSMutableDictionary dictionaryWithCapacity:3];
    [imgDic3 setObject:[UIImage imageNamed:@"my_normal"] forKey:@"Default"];
    [imgDic3 setObject:[UIImage imageNamed:@"my_select"] forKey:@"Highlighted"];
    [imgDic3 setObject:[UIImage imageNamed:@"my_select"] forKey:@"Seleted"];
    NSArray *imgArr = [NSArray arrayWithObjects:imgDic,imgDic2,imgDic3,nil];
    NSArray *titleArr = [NSArray arrayWithObjects:@"首页",@"订单",@"我的" ,nil];
    
    leveyTabBarController = [[LeveyTabBarController alloc] initWithViewControllers:ctrlArr imageArray:imgArr titleArray:titleArr];
    leveyTabBarController.view.backgroundColor = [BPCommon colorWithHexValue:0xeeeeee alpha:1.0f];
    leveyTabBarController.delegate = self;
    leveyTabBarController.tabBar.backgroundColor = [BPCommon colorWithHexValue:0xf7f7f7 alpha:1.0f];
}

- (void)hidesTabBar:(BOOL)yesOrNO animated:(BOOL)animated{
    [leveyTabBarController hidesTabBar:yesOrNO animated:animated];
}

- (BOOL)tabBarController:(LeveyTabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    return YES;
}

- (void)tabBarController:(LeveyTabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

}

//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
//{
//    return [ShareSDK handleOpenURL:url wxDelegate:nil];
//}
//
//- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
//{
//    return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:nil];
//}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
